//
//  LoadingView.m
//  tyzenCoreData
//
//  Created by Nong Nghia on 4/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoadingView.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>

@implementation LoadingView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        
        
        // Initialization code
    }
    return self;
}

-(void)showInitLoadingView{
    NSLog(@"LoadingView-ShowLoadingView");
    
    NSString * firstRun = [[NSUserDefaults standardUserDefaults] valueForKey:@"FirstRun"];
    
    [squareView removeFromSuperview];
    [textLabel removeFromSuperview];
    [indicatorView removeFromSuperview];
    
    squareView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 140)];
    [squareView setCenter:self.center];
    squareView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    squareView.alpha=1;
    squareView.layer.cornerRadius=8;
    squareView.layer.borderWidth=2;
    squareView.layer.borderColor=[UIColor whiteColor].CGColor;
    
    textLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 80, 190, 50)];
    [textLabel setLineBreakMode:UILineBreakModeWordWrap];
    [textLabel setNumberOfLines:2];
    [textLabel setText:@"Initial launch takes a few moments to connect..."];
    [textLabel setTextColor:[UIColor whiteColor]];
    [textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15]];
    [textLabel setTextAlignment:UITextAlignmentCenter];
    [squareView addSubview:textLabel];
    [textLabel setBackgroundColor:[UIColor clearColor]];
    
    [self addSubview:squareView];
    
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [indicatorView setCenter:CGPointMake(self.center.x, self.center.y-10)];
    [indicatorView startAnimating];
    [self addSubview:indicatorView];
    
    self.backgroundColor=[UIColor clearColor];
    UIWindow * main = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
    [main addSubview:self];
}


-(void)showLoadingView{
    [squareView removeFromSuperview];
    [textLabel removeFromSuperview];
    [indicatorView removeFromSuperview];
    
    squareView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 100)];
    [squareView setCenter:self.center];
    squareView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    squareView.alpha=1;
    squareView.layer.cornerRadius=8;
    squareView.layer.borderWidth=2;
    squareView.layer.borderColor=[UIColor whiteColor].CGColor;    
    [self addSubview:squareView];
    
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [indicatorView setCenter:self.center];
    [indicatorView startAnimating];
    [self addSubview:indicatorView];
    
    self.backgroundColor=[UIColor clearColor];
    
    UIWindow * main = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
    [main addSubview:self];
}
-(void)stopLoadingView{
    NSLog(@"LoadingView-StopLoadingView");
    [indicatorView stopAnimating];
    [self removeFromSuperview];
    [self.delegate loadingViewDidStop];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
